//公共js，主要做表单验证，以及基本方法封装
const utils = {
	isNullOrEmpty: function(value) {
		//是否为空
		return (value === null || value === '' || value === undefined) ? true : false;
	},
	trim: function(value) {
		//去空格
		return value.replace(/(^\s*)|(\s+)|(\s*$)/g, "");
	},
	isEmail: function(value) {
		//是否为邮箱
		return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value);
	},
	isMobile: function(value) {
		//是否为手机号
		return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value);
	},
	isFloat: function(value) {
		//金额，只允许保留两位小数
		return /^([0-9]*[.]?[0-9])[0-9]{0,1}$/.test(value);
	},
	isNum: function(value) {
		//是否全为数字
		return /^[0-9]+$/.test(value);
	},
	checkPwd: function(value) {
		//密码为8~20位数字和字母组合
		return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(value);
	},
	setMobile: function(num) {
		//格式化手机号码
		if (utils.isMobile(num)) {
			num = num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
		}
		return num;
	},
	rmoney: function(money) {
		//金额格式化
		return parseFloat(money).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(
			/\,$/, '').split('').reverse().join('');
	},
	nickName: function(value) {
		//昵称校验,1-6个中文
		// return /^([\u4e00-\u9fa5]{2,6})|([A-Za-z0-9_]{2,10})|([a-zA-Z0-9_\u4e00-\u9fa5]{4,10})$/.test(value);
		// return /^[\u4E00-\u9FA5]{1,6}|$/.test(value);
		if (0 < value.length && value.length <= 10) {
			return true
		} else {
			return false
		}
	},
	//保留几位小数
	toFixed: function(value, num) {
		var num = Number(value).toFixed(num);
		return num;
	},
	formatDate: function(fdate, formatStr) {
		//日期格式化
		if (fdate) {

			if (~fdate.indexOf('.')) {
				fdate = fdate.substring(0, fdate.indexOf('.'));
			}
			fdate = fdate.toString().replace('T', ' ').replace(/\-/g, '/');
			var fTime, fStr = 'ymdhis';
			if (!formatStr)
				formatStr = "y-m-d h:i:s";
			if (fdate)
				fTime = new Date(fdate);
			else
				fTime = new Date();
			var month = fTime.getMonth() + 1;
			var day = fTime.getDate();
			var hours = fTime.getHours();
			var minu = fTime.getMinutes();
			var second = fTime.getSeconds();
			month = month < 10 ? '0' + month : month;
			day = day < 10 ? '0' + day : day;
			hours = hours < 10 ? ('0' + hours) : hours;
			minu = minu < 10 ? '0' + minu : minu;
			second = second < 10 ? '0' + second : second;
			var formatArr = [
				fTime.getFullYear().toString(),
				month.toString(),
				day.toString(),
				hours.toString(),
				minu.toString(),
				second.toString()
			]
			for (var i = 0; i < formatArr.length; i++) {
				formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
			}
			return formatStr;
		} else {
			return "";
		}
	},
	// 日补零操作
	dayfill(day) {
		if (day < 10) {
			return (day = String('0' + day));
		} else {
			return (day = String(day));
		}
	},
	//裁剪日期的0
	splitzero(val) {
		if (val < 10) {
			val = parseInt(val)
			return val;
		} else {
			return val;
		}
	},
	// 时间差函数
	timeDifference(startTimeval, endTimeval) {
		let startTime = startTimeval.split(" ")[1];
		let endTime = endTimeval.split(" ")[1];
		let start1 = startTime.split(":");
		let startAll = parseInt(start1[0] * 60) + parseInt(start1[1]);
		let end1 = endTime.split(":");
		let endAll = parseInt(end1[0] * 60) + parseInt(end1[1]);
		return endAll - startAll;
	},
	formatNum(num) {
		if (num < 10) {
			return '0' + num;
		}
		return num;
	},
	// 秒格式化成秒表形式=>0:00:00
	formatSeconds(value) {
		var theTime = parseInt(value); // 秒
		var middle = 0; // 分
		var hour = 0; // 小时

		if (theTime > 60) {
			middle = parseInt(theTime / 60);
			theTime = parseInt(theTime % 60);
			if (middle > 60 || middle == 60) {
				hour = parseInt(middle / 60);
				middle = parseInt(middle % 60);
			}
		}
		var result =
			utils.formatNum(hour) +
			":" +
			utils.formatNum(middle) +
			":" +
			utils.formatNum(theTime);
		return result;
	},
	// 等级
	getrank(value) {
		let rank = "";
		if (value == "excellent") {
			rank = "优秀";
		} else if (value == "good") {
			rank = "良好";
		} else if (value == "pass") {
			rank = "及格";
		} else if (value == "fail") {
			rank = "不及格";
		} else if (value == "normal") {
			rank = "正常";
		} else if (value == "overweigth") {
			rank = "过重";
		} else if (value == "underweigth") {
			rank = "低体重";
		} else if (value == "fat") {
			rank = "肥胖";
		} else if (value == "other") {
			rank = "";
		}
		let obj = {
			rank: rank,
			setClassRank: value,
		};
		return obj;
	},

	//当前日期时间
	getNowDate() {
		var date = new Date();
		var sign2 = ":";
		var year = date.getFullYear() // 年
		var month = date.getMonth() + 1; // 月
		var day = date.getDate(); // 日
		var hour = date.getHours(); // 时
		var minutes = date.getMinutes(); // 分
		var seconds = date.getSeconds() //秒
		var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
		var week = weekArr[date.getDay()];
		// 给一位数的数据前面加 “0”
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (day >= 0 && day <= 9) {
			day = "0" + day;
		}
		if (hour >= 0 && hour <= 9) {
			hour = "0" + hour;
		}
		if (minutes >= 0 && minutes <= 9) {
			minutes = "0" + minutes;
		}
		if (seconds >= 0 && seconds <= 9) {
			seconds = "0" + seconds;
		}
		return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
	},
	uuid() {
		const len = 32; //32长度
		let radix = 16; //16进制
		const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
		let uuid = [],
			i;
		radix = radix || chars.length;
		if (len) {
			for (i = 0; i < len; i++) {
				uuid[i] = chars[0 | (Math.random() * radix)];
			}
		} else {
			let r;
			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
			uuid[14] = '4';
			for (i = 0; i < 36; i++) {
				if (!uuid[i]) {
					r = 0 | (Math.random() * 16);
					uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
				}
			}
		}
		return uuid.join('');
	},
	getGrade(gradeCode) {
		let gradeName = "";
		switch (gradeCode) {
			case '001':
				gradeName = "一年级";
				break;
			case '002':
				gradeName = '二年级';
				break;
			case '003':
				gradeName = '三年级';
				break;
			case '004':
				gradeName = '四年级';
				break;
			case '005':
				gradeName = '五年级';
				break;
			case '006':
				gradeName = '六年级';
				break;
			case '007':
				gradeName = '初一';
				break;
			case '008':
				gradeName = '初二';
				break;
			case '009':
				gradeName = '初三';
				break;
			case '010':
				gradeName = '高一';
				break;
			case '011':
				gradeName = '高二';
				break;
			case '012':
				gradeName = '高三';
				break;
			default:
				gradeName = '';
		}
		return gradeName;
	},

	getGradeList() {
		let list = [{
				gardeCode: '001',
				name: '一年级'
			},
			{
				gardeCode: '002',
				name: '二年级'
			},
			{
				gardeCode: '003',
				name: '三年级'
			},
			{
				gardeCode: '004',
				name: '四年级'
			},
			{
				gardeCode: '005',
				name: '五年级'
			},
			{
				gardeCode: '006',
				name: '六年级'
			},
			{
				gardeCode: '007',
				name: '初一'
			},
			{
				gardeCode: '008',
				name: '初二'
			},
			{
				gardeCode: '009',
				name: '初三'
			},
			{
				gardeCode: '010',
				name: '高一'
			},
			{
				gardeCode: '011',
				name: '高二'
			},
			{
				gardeCode: '012',
				name: '高三'
			}
		]
		return list
	},


	// js字符串转日期Date
	// 字符串格式：2020-10-13 12:00:01
	stringToDate(strDate) {
		var tempStrs = strDate.split(" ");

		var dateStrs = tempStrs[0].split("-");
		var year = parseInt(dateStrs[0], 10);
		var month = parseInt(dateStrs[1], 10) - 1;
		var day = parseInt(dateStrs[2], 10);

		var timeStrs = tempStrs[1].split(":");
		var hour = parseInt(timeStrs[0], 10);
		var minute = parseInt(timeStrs[1], 10);
		var second = parseInt(timeStrs[2], 10);

		var date = new Date(year, month, day, hour, minute, second);
		return date;
	},

	// js日期Date格式化为字符串
	// 字符串格式：2020-10-13 12:00:01
	//fmt :yyyy-MM-dd hh:mm:ss
	dateToStrFormat(date, fmt) {
		var o = {
			"M+": date.getMonth() + 1, //月份
			"d+": date.getDate(), //日
			"h+": date.getHours(), //小时
			"m+": date.getMinutes(), //分
			"s+": date.getSeconds(), //秒
			"q+": Math.floor((date.getMonth() + 3) / 3), //季度
			"S": date.getMilliseconds() //毫秒
		};
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
		}
		for (var k in o) {
			if (new RegExp("(" + k + ")").test(fmt)) {
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ?
					(o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
			}
		}
		return fmt;
	},
	/* 获取当前页url */
	getCurrentPageUrl() {
		const pages = getCurrentPages() // 获取加载的页面
		const currentPage = pages[pages.length - 1] // 获取当前页面的对象
		const url = currentPage.route // 当前页面url
		return url
		// return currentPage
	},

	/* 获取当前页带参数的url */
	getCurrentPageUrlWithArgs() {
		const pages = getCurrentPages() // 获取加载的页面
		const currentPage = pages[pages.length - 1] // 获取当前页面的对象
		const url = currentPage.route // 当前页面url
		const options = currentPage.options // 如果要获取url中所带的参数可以查看options

		// 拼接url的参数
		let urlWithArgs = url + '?'
		for (const key in options) {
			const value = options[key]
			urlWithArgs += key + '=' + value + '&'
		}
		urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

		return urlWithArgs
	},
	//时间戳转字符串
	timeFormate(timeStamp,mode) {
		let year = new Date(timeStamp).getFullYear();
		let month = new Date(timeStamp).getMonth() + 1 < 10 ? "0" + (new Date(timeStamp).getMonth() + 1) : new Date(
			timeStamp).getMonth() + 1;
		let date = new Date(timeStamp).getDate() < 10 ? "0" + new Date(timeStamp).getDate() : new Date(timeStamp).getDate();
		let hh = new Date(timeStamp).getHours() < 10 ? "0" + new Date(timeStamp).getHours() : new Date(timeStamp).getHours();
		let mm = new Date(timeStamp).getMinutes() < 10 ? "0" + new Date(timeStamp).getMinutes() : new Date(timeStamp).getMinutes();
		let ss = new Date(timeStamp).getSeconds() < 10 ? "0" + new Date(timeStamp).getSeconds() : new Date(timeStamp).getSeconds();
		// return year + "-" + month + "-" + date + "" + " " + hh + ":" + mm
		// return year + "-" + month + "-" + date + "" + " " + hh + ":" + mm + ":" + ss
		// return year + "-" + month + "-" + date 
		let dateStr=''
		switch (mode) {
			case '001':
				dateStr =year + "-" + month + "-" + date + "" + " " + hh + ":" + mm;
				break;
			case '002':
				dateStr =  month + "-" + date + "" + " " + hh + ":" + mm;
				break;
			default:
				dateStr =year + "-" + month + "-" + date + "" + " " + hh + ":" + mm + ":" + ss;
		}
		return dateStr
	}





}





// module.exports = {
// 	isNullOrEmpty: utils.isNullOrEmpty,
// 	trim: utils.trim,
// 	isMobile: utils.isMobile,
// 	setMobile:utils.setMobile,
// 	isFloat: utils.isFloat,
// 	isNum: utils.isNum,
// 	checkPwd: utils.checkPwd,
// 	formatNum:utils.formatNum,
// 	rmoney: utils.rmoney,
// 	formatDate: utils.formatDate,
// 	formatSeconds:utils.formatSeconds,
// 	toFixed:utils.toFixed,
// 	dayfill: utils.dayfill,
// 	splitzero:utils.splitzero,
// 	timeDifference:utils.timeDifference,
// 	nickName:utils.nickName,
// 	getrank:utils.getrank,
// }

module.exports = utils;
